import { login, logout } from '@/api/login'
import { getUserPower } from '@/api/userPower.js'
import { Message } from 'element-ui'
import { showPower } from '@/utils/power.js'
// import secret from '@/utils/secret'
import {
  getToken,
  getUserId,
  getUserAvatar,
  getPlatformKey,
  getRoleKey,
  setToken,
  setUser,
  setRole,
  removeToken
} from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    userId: getUserId(),
    avatar: getUserAvatar(),
    platform: getPlatformKey(),
    role: getRoleKey(),
    system: [],
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PLATFORM: (state, platfrom) => {
      state.platfrom = platfrom
    },
    SET_ROLE: (state, role) => {
      state.role = role
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_SYSTEM: (state, system) => {
      state.system = system
    }
  },
  actions: {
    Login({ commit }, param) {
      return new Promise((resolve, reject) => {
        login(param).then(response => {
          // 如果token为空值，拒绝访问
          // console.log(response.data.userId)
          setUser(response.data.userId)
          setRole(response.data.roleName)
          const token = response.data.token
          // console.log(response.token)
          if (!token) {
            Message({
              message: '拒绝访问',
              type: 'error'
            })
            reject()
          } else {
            commit('SET_TOKEN', token)
            // console.log(token)
            setToken(token)
            // console.log(getToken())
            // setUserId(response.userName)

            // setUserAvatar(data.avatar)
            // setPlatformKey(data.platform)
            // setRoleKey(data.role)
            resolve()
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 得到用户权限
    GetUserPower({ commit }) {
      return new Promise((resolve, reject) => {
        getUserPower().then(response => {
          if (response.resultCode !== '0000') {
            reject(response.resultDesc)
            return false
          }
          // 为了安全性，此处不存入token
          const roles = response.data.sysList
          commit('SET_ROLES', roles)
          console.log(roles)
          commit('SET_SYSTEM', showPower(roles))
          resolve(roles)
        }).catch((err) => {
          console.log(err)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}
export default user
